<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" v-model="addressData.name" placeholder="收货人姓名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">手机号</text>
			<input class="input" type="number" v-model="addressData.phone" maxlength="11" placeholder="收货人手机号码" placeholder-class="placeholder" />
		</view>
		<view class="row b-b" >
			<text class="tit">区域</text>
			<text class="input" @click="openAddres">{{pickerText ? pickerText : '请选择区域'}} </text>
			<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm" themeColor='#007AFF'></simple-address>
		</view>
		<view class="row b-b"> 
			<text class="tit">详细描述</text>
			<input class="input" type="text" v-model="addressData.addressDetail" placeholder="地址详细描述" placeholder-class="placeholder" />
		</view>
		
		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="addressData.defualtFlg == '1' " color="#fa436a" @change="switchChange" />
		</view>
		<button class="add-btn" @click="addOrUpdate">提交</button>
		
		<button v-if="addressData.id" class="del-btn" @click="deleteAddress">删除</button>
	</view>
</template>

<script>
	import simpleAddress from "@/components/simple-address/simple-address.nvue"
	export default {
		data() {
			return {
				cityPickerValueDefault: [0, 0, 1],
				pickerText: '',
				addressData: {
					id:'',
					name: '',
					phone: '',
					addressDetail: '',
					address: '',
					defualtFlg: '0',
					longitude:'',
					latitudu:''
				}
			}
		},
		components: {
			simpleAddress
		},
		onLoad(option){
			let id = option.id ? option.id : '';
			this.addressData.id = id;
			let title = '新增收货地址';
			if(id){
				title = '编辑收货地址'
				var that = this;
				that.$network.request("UserAddressAction/getById", {id:id}, function(res) {
					that.addressData = res.data.bean;
					that.pickerText = res.data.bean.address;
				}, "", function(res) {
					console.error(JSON.stringify(res))
					uni.showToast({
						icon: 'none',
						title: '加载失败',
						duration: 1200
					})
				});
			}
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			openAddres() {
			    this.$refs.simpleAddress.open();
			},
			onConfirm(e) {
				this.pickerText = e.label;
				this.addressData.address = this.pickerText;
			},
			deleteAddress(){
				if(this.addressData.defualtFlg == '1'){
					this.$api.msg("默认地址无法删除");
					return;
				}
				var that = this;
				that.$network.request("UserAddressAction/deleted", {id:that.addressData.id}, function(res) {
					if(res.data.state){
						uni.showToast({
							title:'删除成功',
							duration: 1200,
							icon: 'success'
						})
						setTimeout(function(){
							uni.navigateBack({})
						},1200)
					}else{
						that.$api.msg(res.data.msg);
					}
				}, "", function(res) {
					console.error(JSON.stringify(res))
					uni.showToast({
						icon: 'none',
						title: '加载失败',
						duration: 1200
					})
				});
			},
			switchChange(e){
				this.addressData.defualtFlg = e.detail.value == true ? '1' : '0' ;
			},
			/* 添加或者修改地址 */
			addOrUpdate(){
				console.log("adsfadf");
				let that = this;
				let data = this.addressData;
				if(!data.name){
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if(!/(^1[0-9][0-9]{9}$)/.test(data.phone)){
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if(!data.addressDetail){
					this.$api.msg('请填地址详细信息');
					return;
				}
				that.$network.request("UserAddressAction/addOrUpdate", data, function(res) {
					if(res.data.state){
						uni.showToast({
							icon: 'none',
							title: that.addressData.id ? "修改成功" : "添加成功" ,
							duration: 1200
						})
						setTimeout(function(){
							uni.navigateBack({})
						},1200)
					}
				}, "", function(res) {
					console.error(JSON.stringify(res))
					uni.showToast({
						icon: 'none',
						title: '加载失败',
						duration: 1200
					})
				});
			},
			//地图选择地址
			chooseLocation(){
				uni.chooseLocation({
					success: (data)=> {
						this.addressData.addressDetail = data.name;
						this.addressData.address = data.address;
						this.addressData.latitudu = data.latitude;
						this.addressData.longitude = data.longitude;
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: $page-color-base;
		padding-top: 16upx;
	}

	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:0 30upx;
		height: 110upx;
		background: #fff;
		
		.tit{
			flex-shrink: 0;
			width: 140upx;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.input{
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.icon-shouhuodizhi{
			font-size: 36upx;
			color: $font-color-light;
		}
	}
	.default-row{
		margin-top: 16upx;
		.tit{
			flex: 1;
		}
		switch{
			transform: translateX(16upx) scale(.9);
		}
	}
	.add-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
	.del-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 30upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: #d03233;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
